﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ConsoleApplication3
{
    class Program
    {
        const string strConnection
            = "integrated security = true;"
            + "Server = localhost;"
            + "DataBase = SmallBones";

        public class username
        {
            public string Username;
            public bool Exist = false;
            public bool DbError = false;
        };

        public class userid
        {
            public int Userid;
            public bool Exist = false;
            public bool DbError = false;
        }

        

        static username UseridToUsername(int Userid)
        {
            username name = new username();
            SqlConnection con = new SqlConnection(strConnection);
            try
            {
                con.Open();
                string Command = "select name from[SmallBones].dbo.[User] where id =" + Userid.ToString();
                SqlCommand cmd = new SqlCommand(Command, con);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    name.Username = (string)reader[0];
                    name.Exist = true;
                }
                con.Close();
                return name;            
            }
            catch
            {
                name.DbError = true;
                con.Close();
                return name;             
            }
        }

        static userid UsernameToUserid(string username)
        {
           userid id = new userid();
           SqlConnection con = new SqlConnection(strConnection);
           try
           {
               con.Open();
               SqlCommand cmd = new SqlCommand("select id,name from [SmallBones].dbo.[User]", con);
               SqlDataReader reader = cmd.ExecuteReader();
               while (reader.Read())
               {
                   if (username == (string)reader[1])
                   {
                       id.Userid = (int)reader[0];
                       id.Exist = true;
                       con.Close(); reader.Close();
                       return id;
                   }
               }
               reader.Close();
               con.Close();
               return id;
            }
            catch
            {
                id.Exist = false;
                con.Close();
                return id;
            }           
        }

        static void Main(string[] args)
        {
            username user = UseridToUsername(2);
            userid id = UsernameToUserid("wxc");
            Console.WriteLine(user.Username);
            Console.WriteLine(user.Exist);
            Console.WriteLine(user.DbError);
            Console.WriteLine(id.Userid);
            Console.WriteLine(id.Exist);
            Console.WriteLine(id.DbError);
            Console.Read();
        }
    }
}
